package com.scau.controller;

import com.google.common.base.Preconditions;
import com.scau.common.annotations.CurrentUser;
import com.scau.common.message.ResponseMessage;
import com.scau.common.util.MessageUtil;
import com.scau.entity.Counselor;
import com.scau.entity.Student;
import com.scau.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/students")
public class StudentController {

    @Autowired
    private StudentService studentService;

    @RequestMapping(method = RequestMethod.POST)
    public ResponseMessage<String> create(@RequestBody Student student) {

        Preconditions.checkNotNull(student.getName());
        Preconditions.checkNotNull(student.getLinkman());
        student.setRemainingClass(0);
        student.setPoints(0);
        studentService.add(student);

        return MessageUtil.getSuccessMessage();
    }

    @RequestMapping(method = RequestMethod.GET)
    public ResponseMessage<Page<Student>> list(Student student, Pageable pageable, Boolean studying) {

        Page<Student> students = studentService.findAll(student, pageable, studying);

        return MessageUtil.getSuccessMessage(students);
    }

    @RequestMapping(value = "/{studentId}", method = RequestMethod.GET)
    public ResponseMessage<Student> get(@PathVariable Long studentId) {

        Student student = studentService.findOne(studentId);
        return MessageUtil.getSuccessMessage(student);
    }

    @RequestMapping(value = "/{studentId}", method = RequestMethod.PUT)
    public ResponseMessage<Student> update(@PathVariable Long studentId, @RequestBody Student student) {

        student.setId(studentId);
        Student newStudent = studentService.update(student);

        return MessageUtil.getSuccessMessage(newStudent);
    }

    @RequestMapping(value = "/{studentId}", method = RequestMethod.DELETE)
    public ResponseMessage<String> delete(@PathVariable Long studentId) {
        studentService.delete(studentId);

        return MessageUtil.getSuccessMessage("删除成功");
    }

}
